// subPackage/resource/view/index.js
let app = getApp();
import Notify from '@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resource: {},
    showUrl: false,
    showContact: false,
    showMoreBox: false,
    memberDialog: false,
    feedback: {
      resourceId: '',
      resourceName: '',
      feedbackType: '',
      feedbackContent: ''
    },
    showFeedbackBox: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.id;
    app.request('resource/select/by/id', 'POST', {
        id: id
      }, 'xform')
      .then(res => {
        this.setData({
          resource: res.data
        })
      })
    let userinfo = wx.getStorageSync('userinfo');
    this.setData({
      userinfo
    })
  },
  loadResource(id) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toHome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  onClose() {
    this.setData({
      showUrl: false
    })
  },
  copyUrl(e) {
    this.clipboard(this.data.resource.url)
  },
  copy(e) {
    let v = e.currentTarget.dataset.v;
    this.clipboard(v)
    this.hideArea(e)
  },
  clipboard(data) {
    wx.setClipboardData({
      data: data,
      success: function (res) {
        Notify({
          type: 'success',
          message: '复制成功',
          selector: '#notify_selector'
        })
      },
      fail: function (err) {
        Notify({
          type: 'danger',
          message: '复制失败',
          selector: '#notify_selector'
        })
      }
    })
  },

  takeResource() {
    let resource = this.data.resource;
    let userinfo = this.data.userinfo;

    if (resource.type == '免费项目') {
      this.setData({
        showUrl: true
      })
    } else {
      if (userinfo.level == '普通用户') {
        this.setData({
          memberDialog: true
        })
        return;
      }
      let id = resource.id;
      app.request('resource/select/url', "POST", {
        id: id
      }, "xform").then(res => {
        resource.url = res.data;
        this.setData({
          resource: resource,
          showUrl: true
        })
      })
    }
  },
  showAd() {
    let baseInfo = wx.getAppBaseInfo();
    let SDKVersion = baseInfo.SDKVersion;
    let SDKVersions = SDKVersion.split("\.");
    if (SDKVersions[0] > 2 && SDKVersions[1] > 6 && SDKVersions[2] > 0) {
      wx.showToast({
        title: '还未开通激励广告功能',
        icon: "none"
      })
    } else {
      wx.showToast({
        title: '该小程序基础版本号不匹配',
        icon: 'none'
      });
    }
  },
  toCard() {
    wx.switchTab({
      url: '/pages/cardsercet/index',
    })
  },
  showContact() {
    this.setData({
      showContact: true
    })
  },
  cancelContact() {
    this.setData({
      showContact: false
    })
  },
  showMore() {
    this.setData({
      showMoreBox: !this.data.showMoreBox
    });
  },
  showFeedback() {
    let feedback = {
      resourceId: this.data.resource.id,
      resourceName: this.data.resource.subject
    }
    this.setData({
      showMoreBox: false,
      showFeedbackBox: true,
      feedback: feedback
    });
  },
  submitFeedback() {
    let feedbackType = this.data.feedback.feedbackType;
    let feedbackContent = this.data.feedback.feedbackContent;
    if (!feedbackContent || !feedbackType) {
      Notify({
        type: 'danger',
        message: '请输入必填项',
        selector: '#notify_selector'
      })
      return;
    }
    let data = this.data.feedback;
    app.request('resource/insert', 'POST', )
  },
  hideArea(e) {
    let t = e.currentTarget.dataset.t;
    let data = {};
    data[t] = !this.data[t];
    this.setData(data)
  }

})
